home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / progjrn / pj_7_6.arc / SEARCHST.ASM < prev    next >
Assembly Source File  |  1989-10-17  |  3KB  |  68 lines

  1. ;------ SearchString - reveals Microsoft QuickBASIC's INSTR algorithm 
  2.  
  3. ;syntax:  Found = SearchString%(BYVAL StartChar%, Source$, Search$) 
  4. ; where: 
  5. ;   StartChar% specifies where in the source string searching is to begin, 
  6. ;   Source$ is the string being searched, and Search$ is the string to find. 
  7. ;   Found then receives the position in Source$ where Search$ is found 
  8. ;   (1-based), or zero if there was no match. 
  9.  
  10. SearchString Proc Uses SI DI, Start:Word, Source:Word, Search:Word 
  11.  
  12.     Cld                   ;insure that string instructions are forward 
  13.     Push DS               ;assign ES=DS 
  14.     Pop  ES 
  15.     Mov  AX,Start         ;put Start% into AX 
  16.   
  17.     Mov  SI,Search        ;get the address for the Search$ descriptor 
  18.     Mov  DX,[SI]          ;put its length into DX 
  19.     Mov  SI,[SI+02]       ;and its address into SI 
  20.     Dec  DX               ;we'll handle the first character using Scasb 
  21.     Js   Exit             ;they slipped us a null string, so return Start% 
  22.   
  23.     Mov  DI,Source        ;get the address for the Source$ descriptor 
  24.     Mov  CX,[DI]          ;put its length into CX 
  25.     Mov  DI,[DI+02]       ;and its address into DI 
  26.     Mov  BX,DI            ;save that in BX to see how far we searched later 
  27.  
  28.     Dec  AX               ;adjust Start% so 1st character = 0 offset 
  29.     Js   NotFound         ;Start% was zero or negative, get out and return 0 
  30.     Add  DI,AX            ;advance Start% bytes into the string 
  31.     Sub  CX,AX            ;and consider that many fewer characters to search 
  32.     Jbe  NotFound         ;they tried to start past the end 
  33.   
  34.     Lodsb                 ;get and skip over the first character in Search$ 
  35.   
  36. Scan: 
  37.     Repne Scasb           ;find the first character in Source$ that matches 
  38.     Jne  NotFound         ;if it's not there, the complete string isn't either 
  39.     Cmp  CX,DX            ;are we less than LEN(Search$) bytes from the end? 
  40.     Jb   NotFound         ;yes, so there's no point in looking further 
  41.     Or   DX,DX            ;was the string only one character long? 
  42.     Jz   Found            ;yes, so this must be it 
  43.   
  44.     Push SI               ;save the current scanning context 
  45.     Push DI 
  46.     Push CX 
  47.     Mov  CX,DX            ;search LEN(Search$) characters 
  48.     Repe Cmpsb            ;compare the two strings 
  49.     Pop  CX               ;restore the context in case we have to scan again 
  50.     Pop  DI 
  51.     Pop  SI 
  52.     Jne  Scan             ;we didn't find it, keep trying 
  53.   
  54. Found:                    ;we found a match 
  55.     Sub  DI,BX            ;calculate how far into the string we found a match 
  56.     Mov  AX,DI            ;leave the result in AX for the function output 
  57.   
  58. Exit: 
  59.     Ret                   ;return to caller 
  60.   
  61. NotFound: 
  62.     Xor  AX,AX            ;return zero to show we didn't find it 
  63.     Jmp  Short Exit       ;and exit 
  64.   
  65. SearchString Endp 
  66.